﻿#include "../../Common/Common.h"

// 1. 首先定义关键的Windows宏，净化windows.h的包含环境
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h> // 先包含windows.h，让它在我们设定的规则下展开

// 2. 接下来是关键的一步：防止gl.h内部的重复定义
// 定义OpenGL API调用的宏，确保它们使用stdcall调用约定
// 这通常是gl.h所期望的
#ifndef APIENTRY
#define APIENTRY __stdcall
#endif
#ifndef APIENTRYP
#define APIENTRYP APIENTRY *
#endif
#ifndef GLAPI
#define GLAPI extern
#endif

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgGA/GUIEventAdapter>
#include <osgGA/TrackballManipulator>
#include <osgViewer/ViewerEventHandlers>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>

#include <iostream>

// Output A Node
void ExportANode()
{
	osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("cow.osg");
	osg::ref_ptr<osg::MatrixTransform> max = new osg::MatrixTransform;

	max->addChild(node.get());
	max->setMatrix(osg::Matrix::translate(50.0, 0.0, 0.0));

	osgDB::ReaderWriter::WriteResult result = osgDB::Registry::instance()->writeNode(*max.get(), "TrCow.osg", osgDB::Registry::instance()->getOptions());
	osg::setNotifyLevel(osg::NOTICE);	// 设置输出级别
	if (result.success())
	{
		osg::notify(osg::NOTICE) << "Write Node Success" << std::endl;
	}

}

// Matrix Operation
osg::ref_ptr<osg::Node> MatrixOperation()
{
	// MatrixTransform is a subclass of Transform which has an osg::Matrix which represents a 4x4 transformation
	// of its children from local coordinates into the Transform's parent coordinates. 
	osg::ref_ptr<osg::Group> group = new osg::Group;
	osg::ref_ptr<osg::MatrixTransform> max = new osg::MatrixTransform;


	max->addChild(osgDB::readNodeFile("glider.osg"));
	max->setMatrix(osg::Matrix::translate(5.0, 0.0, 0.0));

	group->addChild(osgDB::readNodeFile("avatar.osg"));
	group->addChild(max.get());

	return group;
}

int main()
{
	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
	osg::ref_ptr<osg::PositionAttitudeTransform> pat = new osg::PositionAttitudeTransform;

	//viewer->setSceneData(MatrixOperation().get());
	//ExportANode();
	osg::ref_ptr<osg::Group> group = new osg::Group;
	group->addChild(osgDB::readNodeFile("glider.osg"));
	pat->addChild(osgDB::readNodeFile("TrCow.osg"));
	pat->setPosition(osg::Vec3d(-50, 0, 0));

	group->addChild(pat.get());

	viewer->setSceneData(group.get());

	// 设置为单屏幕模式
	viewer->setUpViewInWindow(100, 100, 1920, 1080); // 在主屏幕上创建窗口

	return viewer->run();
}
